#!/bin/bash

# 彩色输出函数
function echo_color() {
    local color=$1
    local text=$2
    case $color in
        "red") echo -e "\033[31m$text\033[0m" ;;
        "green") echo -e "\033[32m$text\033[0m" ;;
        "yellow") echo -e "\033[33m$text\033[0m" ;;
        "blue") echo -e "\033[34m$text\033[0m" ;;
        *) echo "$text" ;;
    esac
}

# 检查端口占用
function check_port() {
    local port=$1
    if lsof -i:$port > /dev/null 2>&1; then
        echo_color "red" "端口 $port 已被占用！"
        return 1
    fi
    return 0
}

# 设置数据库
echo_color "blue" "=================================="
echo_color "blue" "开始设置数据库..."
echo_color "blue" "=================================="

# 创建数据库和用户（需要用户输入MySQL root密码）
echo_color "yellow" "请输入MySQL root密码以创建数据库和用户："
mysql -u root -p -e "source /home/todo/checkin/create_database.sql"

if [ $? -eq 0 ]; then
    echo_color "green" "数据库设置成功！"
else
    echo_color "red" "数据库设置失败，请检查密码是否正确！"
    echo_color "yellow" "您也可以手动运行以下命令设置数据库："
    echo_color "yellow" "mysql -u root -p < /home/todo/checkin/create_database.sql"
    exit 1
fi

# 构建后端
echo_color "blue" "=================================="
echo_color "blue" "开始构建后端项目..."
echo_color "blue" "=================================="
cd /home/todo/checkin/backend

# 检查并构建Maven项目
if [ -f "pom.xml" ]; then
    echo_color "yellow" "正在使用Maven构建项目..."
    mvn clean package -DskipTests
    
    if [ $? -eq 0 ]; then
        echo_color "green" "后端构建成功！"
    else
        echo_color "red" "后端构建失败！"
        exit 1
    fi
else
    echo_color "red" "未找到pom.xml文件！"
    exit 1
fi

# 构建前端
echo_color "blue" "=================================="
echo_color "blue" "开始构建前端项目..."
echo_color "blue" "=================================="
cd /home/todo/checkin/frontend

# 安装依赖并构建
if [ -f "package.json" ]; then
    echo_color "yellow" "正在安装前端依赖..."
    npm install --legacy-peer-deps
    
    if [ $? -eq 0 ]; then
        echo_color "yellow" "正在构建前端项目..."
        npm run build
        
        if [ $? -eq 0 ]; then
            echo_color "green" "前端构建成功！"
        else
            echo_color "red" "前端构建失败！"
            exit 1
        fi
    else
        echo_color "red" "前端依赖安装失败！"
        exit 1
    fi
else
    echo_color "red" "未找到package.json文件！"
    exit 1
fi

# 启动后端服务
echo_color "blue" "=================================="
echo_color "blue" "启动后端服务..."
echo_color "blue" "=================================="
cd /home/todo/checkin/backend

# 检查端口是否被占用
check_port 8080
if [ $? -ne 0 ]; then
    echo_color "red" "无法启动后端服务，端口8080已被占用！"
    exit 1
fi

check_port 8090
if [ $? -ne 0 ]; then
    echo_color "red" "无法启动WebSocket服务，端口8090已被占用！"
    exit 1
fi

# 启动后端服务（后台运行）
nohup java -jar target/*.jar > backend.log 2>&1 &
BACKEND_PID=$!

echo_color "green" "后端服务已启动，PID: $BACKEND_PID"
echo_color "yellow" "日志文件: /home/todo/checkin/backend/backend.log"

# 等待后端服务启动
sleep 5

# 启动前端静态服务器
echo_color "blue" "=================================="
echo_color "blue" "启动前端静态服务器..."
echo_color "blue" "=================================="
cd /home/todo/checkin/frontend/dist

# 检查80端口是否被占用
check_port 80
if [ $? -ne 0 ]; then
    echo_color "yellow" "端口80已被占用，使用8081端口..."
    PORT=8081
else
    PORT=80
fi

# 使用Python启动简单的HTTP服务器
if command -v python3 &> /dev/null; then
    nohup python3 -m http.server $PORT > /home/todo/checkin/frontend/frontend.log 2>&1 &
    FRONTEND_PID=$!
    
    echo_color "green" "前端静态服务器已启动，PID: $FRONTEND_PID"
    echo_color "green" "前端访问地址: http://localhost:$PORT"
    echo_color "yellow" "前端日志文件: /home/todo/checkin/frontend/frontend.log"
else
    echo_color "red" "未找到Python3，无法启动前端静态服务器！"
    exit 1
fi

echo_color "green" "=================================="
echo_color "green" "部署完成！"
echo_color "green" "后端API地址: http://localhost:8080/api"
echo_color "green" "前端访问地址: http://localhost:$PORT"
echo_color "green" "=================================="
echo_color "yellow" "停止服务命令: kill $BACKEND_PID $FRONTEND_PID"